package com.yoda.youdianda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.youdianda.common.RC;
import com.yoda.youdianda.common.ThreadContext;
import com.yoda.youdianda.domain.YddCate;
import com.yoda.youdianda.dto.YddArticleDto;
import com.yoda.youdianda.service.YddCateService;
import com.yoda.youdianda.util.PageUtil;
import com.yoda.youdianda.common.R;
import com.yoda.youdianda.domain.YddArticle;
import com.yoda.youdianda.service.YddArticleService;
import com.yoda.youdianda.vo.ArticleVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


//文章管理
@Slf4j
@RestController
@RequestMapping("/ydd_article")
public class YddArticleController {
    @Autowired
    private YddArticleService yddArticleService;


    @Autowired
    private YddCateService yddCateService;


    //分页条件查询
    @GetMapping("/list")
    private R<PageUtil> list(Long start_time, Long end_time, String title, Integer cateid, int page, int limit) {
        //构建分页查询对象
        Page<YddArticle> yddArticlePage = new Page<>(page, limit);
        Page<YddArticleDto> articleVoPage = new Page<>();
        LambdaQueryWrapper<YddArticle> yddArticleWrapper = new LambdaQueryWrapper<>();
        //添加标题模糊查询条件
        yddArticleWrapper.like(StringUtils.isNotEmpty(title), YddArticle::getTitle, title)
                //添加分类精确查询条件
                .eq(Objects.nonNull(cateid), YddArticle::getCateid, cateid)
                //添加时间戳查询小于开始时间查询条件
                .le(Objects.nonNull(start_time), YddArticle::getCreateDate, start_time)
                //添加大于结束时间查询条件
                .ge(Objects.nonNull(end_time), YddArticle::getUpdateDate, end_time);
        //添加排序条件
        yddArticleWrapper.orderByDesc(YddArticle::getCreateDate).orderByAsc(YddArticle::getId);
        //执行分页查询
        yddArticleService.page(yddArticlePage, yddArticleWrapper);
        //对象拷贝
        BeanUtils.copyProperties(yddArticlePage, articleVoPage, "records");
        List<YddArticle> records = yddArticlePage.getRecords();
        List<YddArticleDto> list = records.stream().map((arr) -> {

            YddArticleDto dto = new YddArticleDto();
            BeanUtils.copyProperties(arr,dto);
            Integer cateId = arr.getCateid();
            YddCate cate = yddCateService.getById(cateId);
            dto.setCatename(cate.getCatename());
            return dto;
        }).collect(Collectors.toList());
        articleVoPage.setRecords(list);
        PageUtil<YddArticleDto> yddArticleDtoPageUtil = PageUtil.pageUtil(articleVoPage);
        return R.success(yddArticleDtoPageUtil);
    }

    @PostMapping("/add")
    public R<String> add(@RequestBody YddArticle yddArticle) {
        yddArticle.setUserId(136);
        yddArticleService.save(yddArticle);
        return R.success("添加成功");
    }

    //数据回显
    @GetMapping("/rowInfo")
    public R rowInfo(@RequestParam int id) {
        YddArticle yddArticle = yddArticleService.getById(id);
        return R.success(yddArticle);

    }

    //修改
    @PostMapping("/update")
    public R<String> update(@RequestBody YddArticle yddArticle) {
        yddArticleService.updateById(yddArticle);
        return R.success("修改成功");
    }

    //删除文章
    @GetMapping("del")
    public R<String> delete(Integer id) {
        yddArticleService.removeById(id);
        return R.success("删除成功");
    }
}
